<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--插槽:table标题-->
      <template #tableTitle>
          <a-button  type="primary" @click="openModal(true,{})" preIcon="ant-design:hdd-outlined"> 回收站</a-button>
          <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="2" @click="batchFrozen(2)">
                <Icon icon="ant-design:lock-outlined"></Icon>
                冻结
              </a-menu-item>
              <a-menu-item key="3" @click="batchFrozen(1)">
                <Icon icon="ant-design:unlock-outlined"></Icon>
                解冻
              </a-menu-item>
            </a-menu>
          </template>
          <a-button>批量操作
            <Icon icon="mdi:chevron-down"></Icon>
          </a-button>
        </a-dropdown>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)"/>
      </template>
    </BasicTable>
    <!--用户抽屉-->
    <DetailDrawer @register="registerDrawerForDetail"/>
    <!--修改密码-->
    <PasswordModal @register="registerPasswordModal" @success="reload"/>
    <!--回收站-->
    <UserRecycleBinModal @register="registerModal" @success="reload"/>
  </div>
</template>

<script lang="ts" name="system-user" setup>
  //ts语法
  import {ref, computed, unref} from 'vue';
  import { BasicTable, TableAction, ActionItem } from '/@/components/Table'
  import DetailDrawer from './DetailDrawer.vue';
  import UserRecycleBinModal from './UserRecycleBinModal.vue';
  import PasswordModal from './PasswordModal.vue';
  import {useDrawer} from '/@/components/Drawer';
  import { useListPage } from '/@/hooks/system/useListPage'
  import {useModal} from '/@/components/Modal';
  import {useMessage} from '/@/hooks/web/useMessage';
  import {columns, searchFormSchema} from './user.data';
  import {list, batchUpdateStatus} from './user.api';
  // import { usePermission } from '/@/hooks/web/usePermission'
  // const { hasPermission } = usePermission();

  const {createMessage, createConfirm} = useMessage();

  const selectRows = ref([]);
  // 注册drawer
  const [registerDrawerForDetail, {openDrawer: openDrawerForDetail}] = useDrawer();
  // 回收站model
  const [registerModal, {openModal}] = useModal();
  //密码model
  const [registerPasswordModal, {openModal: openPasswordModal}] = useModal();

  // 列表页面公共参数、方法
  const { prefixCls, tableContext } = useListPage({
    designScope: 'user-list',
    tableProps: {
      title: '用户列表',
      api: list,
      columns: columns,
      size: 'small',
      formConfig: {
        labelWidth: 200,
        schemas: searchFormSchema,
      },
      actionColumn: {
        width: 120,
      },
      beforeFetch:(params)=>{
        return Object.assign({'column':'createTime','order':"desc"},params)
      },
    }
  })


  //注册table数据
  const [registerTable, {reload, updateTableDataRecord},{ rowSelection, selectedRowKeys }] = tableContext

  /**
   * 详情
  */
  async function handleDetail(record: Recordable) {
    openDrawerForDetail(true, {
      record,
      showFooter: false,
    });
  }

  /**
   * 打开修改密码弹窗
   */
  function handleChangePassword(userName, id){
    openPasswordModal(true, {userName, id});
  }
  /**
   * 冻结解冻
   */
  async function handleFrozen(record,status){
    if ('admin' == record.userName) {
      createMessage.warning('管理员账号不允许此操作！');
      return;
    }
    await batchUpdateStatus({ids: record.userId, status: status},reload)
  }
   /**
   * 批量冻结解冻
   */
  function batchFrozen(status) {
    let hasAdmin = unref(selectRows).filter(item => item.userName == 'admin');
    if (unref(hasAdmin).length > 0) {
      createMessage.warning('管理员账号不允许此操作！');
      return;
    }
    createConfirm({
      iconType: 'warning',
      title: '确认操作',
      content: "是否" + (status == 1 ? "解冻" : "冻结") + "选中账号?",
      onOk: async () => {
        await batchUpdateStatus({ids: unref(selectedRowKeys).join(','), status: status}, reload)
      }
    })
  }

  /**
   * 操作栏
   */
  function getTableAction(record): ActionItem[] {
    return [
      {
        label: '详情',
        onClick: handleDetail.bind(null, record),
        // ifShow: () => hasPermission('user:edit'),
      }, 
    ]
  }
  /**
   * 下拉操作栏
   */
  function getDropDownAction(record): ActionItem[] {
    return [
      {
        label: '密码',
        onClick: handleChangePassword.bind(null, record.userName, record.id),
      },
      {
        label: '冻结',
        ifShow:record.status==1,
        popConfirm: {
          title: '确定冻结吗?',
          confirm: handleFrozen.bind(null, record, "2"),
        },
      },{
        label: '解冻',
        ifShow:record.status==2,
        popConfirm: {
          title: '确定解冻吗?',
          confirm: handleFrozen.bind(null, record, "1"),
        },
      }
    ]
  }
</script>

<style scoped>

</style>
